package androcr.client.mobile.services.translator;

import androcr.client.mobile.R;
import androcr.client.mobile.core.Controller;
import androcr.client.mobile.display.translator.ITranslatorActivity;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class TranslatorPresenter implements ITranslatorPresenter {
	private static final String TAG = "TranslatorPresenter";
	
	private ITranslatorActivity translatorActivity;
	
	public TranslatorPresenter(ITranslatorActivity translatorActivity) {
		this.translatorActivity = translatorActivity;
		
	}

	@Override
	public CharSequence getTextToTranslate() {
		return Controller.getInstance().getActualRecognizedText();
	}
	
	@Override
	public void translateString(String text){
	    // Set the HTTP referrer to website address.
	    Translate.setHttpReferrer("www.google.com");
	    
	    String translatedText;
	    String from = translatorActivity.getSpinnerFromLang().getSelectedItem().toString();
	    from = from.substring(from.length()-2);
	    String to = translatorActivity.getSpinnerToLang().getSelectedItem().toString();
	    to = to.substring(to.length()-2);
	    Log.i(TAG, "translatedtextxxxxxxxxxxxxxx: " + from + "_" + to);
	    Language fromLang = Language.fromString(from);
	    Language toLang = Language.fromString(to);
		try {
			translatedText = Translate.execute(text, fromLang, toLang);
			translatorActivity.getEditTextOutputText().setText(translatedText);
			Log.i(TAG, "translatedtext: " + translatedText);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    //System.out.println(translatedText);
	  }

	@Override
	public void fillSpinnersWithLanguages() {
		ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource((Context)translatorActivity, R.array.language_array, android.R.layout.simple_spinner_item);
	    adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    translatorActivity.getSpinnerFromLang().setAdapter(adapterFrom);
	    
	    ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource((Context)translatorActivity, R.array.language_array, android.R.layout.simple_spinner_item);
	    adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    translatorActivity.getSpinnerToLang().setAdapter(adapterTo);
	}
}
